package chapter17;

import javax.swing.*;
import java.awt.*;

public class Histogram extends JPanel{
	/*显示排名的GUI类*/
	private int[] count;
	
	public void showHistogram(int[] count){
		this.count = count;
		repaint();
	}
	
	protected void paintComponent(Graphics g){
		if (count == null) return;
		super.paintComponent(g);
		
		int width = getWidth();
		int height = getHeight();
		int interval = (width - 40) / count.length;
		int individualWidth = (int) (((width - 10) / 24) * 0.60);
		System.out.printf("width=%d\n",width);
		System.out.printf("height=%d\n", height);
		System.out.printf("interval=%d\n", interval);
		System.out.printf("individualWidth=%d\n", individualWidth);
		int maxCount = 0;
		for (int i = 0; i < count.length; i++) {
			if (maxCount < count[i])
				maxCount = count[i];
		}
		
		int x = 30;
		g.drawLine(10, height - 45, width - 10, height - 45);
		for (int i = 0; i < count.length; i++) {
			System.out.println("*************************************");
			int barHeight = (int) (((double) count[i] / (double) maxCount) * (height - 55));
			System.out.printf("barHeight=%d\n", barHeight);
			g.drawRect(x, height - 45 - barHeight, individualWidth, barHeight);
			System.out.printf("x=%d\n", x);
			g.drawString((char) (65 + i) + "", x, height - 30);
			
			x += interval;
			System.out.println("*************************************");
		}
	}
	
	public Dimension getPerferredSize() {
		return new Dimension(300, 300);
	}
	

}
